package org.example.ymcc.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.ymcc.constant.RConst;
import org.example.ymcc.domain.CourseType;
import org.example.ymcc.mapper.CourseTypeMapper;
import org.example.ymcc.result.JsonResult;
import org.example.ymcc.service.ICourseTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.ymcc.vo.CourseTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程目录 服务实现类
 * </p>
 *
 * @author zcj
 * @since 2024-06-11
 */
@Service
public class CourseTypeServiceImpl extends ServiceImpl<CourseTypeMapper, CourseType> implements ICourseTypeService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public static final String COURSE_TYPE_TREE = "CourseTypeTree";

    @Cacheable(cacheNames="books",key="'book1'")
    @Override
    public JsonResult getTree() {
        if(redisTemplate.hasKey(COURSE_TYPE_TREE)){
            Object obj = redisTemplate.opsForValue().get(COURSE_TYPE_TREE);
            return JsonResult.success().setData(obj);
        }else {
            //forMap的方案
            List<CourseType> list = super.list();
            List<CourseType> top = new ArrayList<>();
            //转换
/*        Map<Long,CourseType> courseMap = new HashMap<>(list.size());
        for (CourseType courseType : list) {
            courseMap.put(courseType.getId(),courseType);
        }*/
            Map<Long, CourseType> courseMap = list.stream()
                    .collect(Collectors.toMap(CourseType::getId, t -> t));
            list.forEach(type->{
                if(RConst.Num.LONG_0.equals(type.getPid())){
                    top.add(type);
                }else {
                    CourseType pType = courseMap.get(type.getPid());
                    pType.getChildren().add(type);
                }
            });
            redisTemplate.opsForValue().set(COURSE_TYPE_TREE,top);
            return JsonResult.success().setData(top);
        }
    }

    @Override
    public JsonResult loadCrumbs(Long courseTypeId) {
        //代码怎么写
        CourseType byId = super.getById(courseTypeId);//当前的数据
        //有几层
        String[] split = byId.getPath().split("\\.");
        List<CourseTypeVo> list = new ArrayList<>(split.length);
        for (String s : split) {
            //自己
            CourseTypeVo courseTypeVo = new CourseTypeVo();
            CourseType courseType = super.getById(Long.valueOf(s));
            courseTypeVo.setCourseType(courseType);
            //兄弟
            Long pid = courseType.getPid();
            courseTypeVo.setCourseTypes(super.list(new LambdaQueryWrapper<CourseType>().eq(CourseType::getPid, pid)));
            list.add(courseTypeVo);
        }
        //返回是一个list
        return JsonResult.success().setData(list);
    }

    //    @CacheEvict(cacheNames="books")
    @Override
    public boolean save(CourseType entity) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return super.save(entity);
    }

    @Override
    public boolean updateById(CourseType entity) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        redisTemplate.delete(COURSE_TYPE_TREE);
        return super.removeById(id);
    }
}
